<?php

//    Hearthstone PHP Class.

//    Copyright (C) 2009 Kirth 'Sebastian'.
//
//    This code is property of the CodeArray PHP project.  Rights Reserved!
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

class HearthStone {

	private $host;
	private $user;
	private $pass;
	private $logon;
	private $world;
	private $chars;
	
	private	$mysql_connect;
	private $error_report	= false;
	
	function __construct($dbhost, $dbuser, $dbpass, $logon, $world, $chars) {
	// 1. Assign the variables.
		$this->host  = $dbhost;
		$this->user  = $dbuser;
		$this->pass  = $dbpass;
		$this->logon = $logon;
		$this->chars = $chars;
		$this->world = $world;
	// 2. Establish MySQL connection!
		$this->mysql_connect = mysql_connect($this->hsdb_host, $this->user, $this->pass)
							   or die("An MySQL Error Ocurred: " . mysql_error());		
	}
	
	
	
	
	// Public functions.
	
	function SetErrorReporting($bool) {
		if(!is_bool($bool)) return false;
			$this->error_report = $bool;
	}
	
	
	function PlayerOnline($nickname) {
	// <summary>
	//		Returns an bool, it's value depending wether the char
	//		is online or not. (1 = true; 0 = false).
	// </summary>
	mysql_select_db($this->chars);	
		$csql .= "SELECT online FROM characters";
		$csql .= " WHERE name = '";
		$csql .= $nickname;
		$csql .= "';";
		if($c = mysql_query($csql)) {
		// Does the character exist?
		if(mysql_num_rows($c) <= 0) return false;
		// Check wether the character is logged in.
			if($r = mysql_fetch_array($c))
				if($r[0] == "1") return true; // It is!
			elseif($this->error_report)
				echo "MySQL Error: " . mysql_error();
		} else {
			if($this->error_report)
				echo "MySQL Error: " . mysql_error();
		}
		
		return false;	// Character was not logged in..
	}
	
	
	function PlayersPlaying() {
	// <summary>
	//		Returns an (int) containing the amount of players Online.
	// </summary>
	mysql_select_db($this->chars);
	
		$c = mysql_query("SELECT name FROM characters WHERE online = '1';");
			return mysql_num_rows($c);

		if($this->error_reporting)
			echo "MySQL Error: " . mysql_error();
	}
	
	
	function CharacterExists($character) {
	// <summary>
	//		Returns a bool to define wether the char exists.
	// </summary>
	mysql_select_db($this->chars);
	
		$c = mysql_query("SELECT acct FROM characters WHERE name = '".$character."';");
		if(mysql_num_rows($c) > 0)
			return true;	// Character(s?) found!
		else
			return false;	// No chars found!
	}
	
	
	function GetCharacterInfo($character) {
	// <summary>
	//		Returns an array with character info.
	// </summary>
	mysql_select_db($this->chars);
	$c;	// SQL Query Placeholder.
	$carray = array(
								"guid" => 0,
								"name" => "",
								"level" => 0,
								"acct" => 0,
								"race" => 0,
								"class" => 0,
								"gender" => 0,
								"gold" => 0,
								"banned" => 0,
								"isResting" => 0,
								"deathState" =>  0,
								"honorPoints" => 0,
							);
		if(is_numeric($character)) // Support for the use of GUID's instead of charnames.
			$c = mysql_query("SELECT * FROM characters WHERE guid = '".$character."';");
		else
			$c = mysql_query("SELECT * FROM characters WHERE name = '".$character."';");
		
		$r = mysql_fetch_array($c, MYSQL_ASSOC);
		
		// Assigning the array's values.
		$carray['guid'] 		= $r['guid'];
		$carray['name'] 		= $r['name'];
		$carray['level'] 		= $r['level'];
		$carray['race'] 		= $r['race'];
		$carray['class'] 		= $r['class'];
		$carray['gender'] 		= $r['gender'];
		$carray['gold'] 		= $r['gold'];
		$carray['banned'] 		= $r['banned'];
		$carray['isResting'] 	= $r['isResting'];
		$carray['deathState'] 	= $r['deathstate'];
		$carray['honorPoints'] 	= $r['honorPoints'];
		
			return $carray;
	}
	
	
	function HasAchievement($character, $achievement) {
	// <summary>
	//		Returns a bool to define wether the char has the achievement.
	// </summary>
	mysql_select_db($this->chars);
	$c;
		
		if(!is_numeric($achievment)) return false;
		
		if(is_numeric($character)) return false;
		
		$c = mysql_query("SELECT * FROM achievements
						  WHERE player = '".$character."' AND
						  achievementid = '".$achievements."' AND
						  completed = '1';");
						  
		if(mysql_num_rows($c) > 0) return true;
		
			return false;
	}
	
	function IsDead($character) {
	// <summary>
	//		Returns a bool to define wether the char is dead.
	// </summary>
	mysql_select_db($this->chars);
	$c;
		if(is_numeric($character))
			$c = mysql_query("SELECT deathstate FROM characters WHERE guid = '".$character."';");
		else
			$c = mysql_query("SELECT deathstate FROM characters WHERE name = '".$character."';");
			
		$r = mysql_fetch_assoc($c);
		
		if($r[0] == "1") return true;
		
			return false;
	}
	
	
	function HasItem($character, $itemid) {
	// <summary>
	//		Returns a bool to define wether the char has the given item.
	// </summary>
	mysql_select_db($this->chars);
	$c;
		if(!is_numeric($itemid)) return false;
		if(!is_numeric($character)) return false;
		
		$c = mysql_query("SELECT guid FROM playeritems
						  WHERE ownerguid = '".$character."'
						  AND entry = '".$itemid."';");
		if(mysql_num_rows($c) > 0)
			return true;
			
		return false;		
	}
	
	/*
	 *  ACCOUNT FUNCTIONS.
	 * These operate through the logon-db.
	 *
	 */
	
	
	function AccountExists($account) {
	// <summary>
	//		Returns a bool to define wether the account exists.
	// </summary>
	mysql_select_db($this->logon);
	
		$c = mysql_query("SELECT acct FROM accounts WHERE login = '".$account."';");
		if(mysql_num_rows($c) > 0)
			return true;	// Account(s?) found!
		else
			return false;	// No account found!
	}
	
	
	function IsBanned($account) {
	// <summary>
	//		Returns a bool to define wether the account is banned.
	// </summary>
	mysql_select_db($this->logon);
	$c;
		if(is_numeric($account))
			$c = mysql_query("SELECT banned FROM accounts WHERE acct = '".$account."';");
		else
			$c = mysql_query("SELECT banned FROM accounts WHERE login = '".$account."';");
			
		$r = mysql_fetch_array($c, MYSQL_ASSOC);
		if($r[0] == "1") return true;
		
			return false;
	}
	
	/*
	 *	STATIC FUNCTIONS
	 *  These can be called witouth calling
	 *  the class.. Mostly help functions.
	 */
	 
	 
	static function IntToRace($race) {
	// <summary>
	//		Returns a string from the raceid.
	// </summary>
		if(!is_numeric($race)) return false;
		
		switch($race)
		{
			case 1: 	return "Human"; 	break;
			case 2: 	return "Orc"; 		break;
			case 3: 	return "Dwarf"; 	break;
			case 4: 	return "Nightelf"; 	break;
			case 5: 	return "Undead"; 	break;
			case 6: 	return "Tauren"; 	break;
			case 7: 	return "Gnome"; 	break;
			case 8: 	return "Troll"; 	break;
			case 10: 	return "Bloodelf"; 	break;
			case 11: 	return "Draenei"; 	break;
		
			default: return "N/A"; break;
		}
		
	}
	
	
	static function IntToClass($class) {
	// <summary>
	//		Returns a string from the classid.
	// </summary>
		if(!is_numeric($class)) return false;
		
		switch($class)
		{
			case 1: 	return "Warrior"; 	break;
			case 2: 	return "Paladin"; 	break;
			case 3: 	return "Hunter"; 	break;
			case 4: 	return "Rogue"; 	break;
			case 5: 	return "Priest"; 	break;
			case 7: 	return "Shaman"; 	break;
			case 8: 	return "Mage"; 		break;
			case 9: 	return "Warlock"; 	break;
			case 11: 	return "Druid"; 	break;
			
			default: return "N/A";			break;
		
		}
	}
	
	static function IntToMap($map) {
	// <summary>
	//		Returns a string from the mapid.
	// </summary>
		if(!is_numeric($map)) return false;
		
		switch($map)
		{
			case 0: 	return "Eastern Kingdoms"; 	break;
			case 1: 	return "Kalimdor"; 			break;
			case 530: 	return "Outland"; 			break;
			default: return "N/A"; break;
		}
	}
	
	

}

?>

<?php
/*
	// Example useage..
	// You can now find an example in the example directory!

	$hs = new HearthStone("127.0.0.1", "root", "", "hs_logon", "hs_world", "hs_chars");
	$hs->SetErrorReporting(true);
	if(!$hs->PlayerOnline("Hank"))
		echo "Hank is not online..";
	else
		echo "Hank is online!";
		
	echo "There are " . $hs->PlayersPlaying() . " people playing!";
*/	
?>